package com.ternnetwork.configuration.controller.persistence;

import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.persistence.dao.IBaseDao;




@Controller@Scope("prototype")
@RequestMapping("/configuration/system/sql/execute/*")
public class SQLExecuteController {
	
	@Resource
	private IBaseDao iBaseDao;
	
	
	@RequestMapping("index.htm")
	public ModelAndView execute(){
		return new ModelAndView("/WEB-INF/view/configuration/persistence/sql_execute.jsp");
	}
	
	@RequestMapping("bulkUpdate.htm")
	public @ResponseBody BaseResponse  bulkUpdate(String sql){
		BaseResponse resp=new BaseResponse();
		try{
			String[] array=sql.split(";");
			int totalCount=0;
			for(String singleSql:array){
				if(!StringUtils.isEmpty(singleSql)){
					totalCount=totalCount+iBaseDao.bulkUpdateByNativeSQL(singleSql.trim(), null);
				}
			}
			resp.setStatus(1);
			resp.setMessage(String.valueOf(totalCount));
			return resp;
		}catch(Exception e){
			e.printStackTrace();
			resp.setStatus(0);
			resp.setMessage(e.getMessage());
			return resp;
		}
	}
	

}
